<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Sys_Role;
use App\Services\UserService;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;

class UserController extends Controller
{
    public function userAuth()
    {
        $user = JWTAuth::parseToken()->authenticate();
//        $arr = json_decode($user->hasOneRole->permissions,true)['permissions'];
//
//        $user->permissions = $this->convertTree($arr);
//        $user = $user->makeHidden(['hasOneRole']);
        return $this->responseSuccess($user);
    }

    public function getUserList(UserService $userService , Request $request)
    {
        $params = $request->input();
        $rid = $this->getRid();
        if($rid != 1){
            $params['uid'] = $this->getUid();
        }
        $list = $userService->getUserList($params);
        return $this->responseSuccess($list);
    }

    public function userList(UserService $userService , Request $request)
    {
        $params = $request->input();
        $rid = $this->getRid();
        if($rid != 1){
            $params['uid'] = $this->getUid();
        }
        $list = $userService->getList($params);
        return $this->responseSuccess( $list['data']);
    }

    public function userAdd(UserService $userService , Request $request)
    {
        $params = $request->input();
        $result = $userService->addUser($params);
        if($result)
            return $this->responseSuccess($result);
        else
            return $this->responseError("添加失败，请重试！");
    }

    public function userDel(UserService $userService , Request $request)
    {
        $params = $request->input();
        $result = $userService->delUser($params);
        if($result)
            return $this->responseSuccess($result);
        else
            return $this->responseError("添加失败，请重试！");
    }
}
